package com.iraeta.controller;

import com.iraeta.service.FangKeService;
import com.iraeta.uitl.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@EnableScheduling
@EnableAsync
public class GlobalTask {
    private static final Logger logger = LoggerFactory.getLogger(GlobalTask.class);

    @Autowired
    FangKeService fangKeService;

    //每8秒获取下海康待审核的数据
    @Scheduled(fixedDelay = 8000)
    @Async("cstaskexecutor")
    public void getHikFangKeRecord() {
        try {
            fangKeService.getFangKeRecordFromHikApi();
        }catch (Exception e){
            logger.error(Util.toStackTrace(e));
        }
    }

    //每8秒获取系统处理待审核的数据
    @Scheduled(fixedDelay = 8000)
    @Async("cstaskexecutor")
    public void processHikFangKeRecord() {
        try {
            fangKeService.processFangKeRecord();
        }catch (Exception e){
            logger.error(Util.toStackTrace(e));
        }
    }

    /*凌晨4点执行 删除历史数据*/
    @Scheduled(cron = "0 0 4 * * ?")
    @Async("cstaskexecutor")
    public void deleteHisRecord() {
        try {
            fangKeService.deleteHisFangKeRecord();
        }catch (Exception e){
            logger.error(Util.toStackTrace(e));
        }
    }

    /*凌晨5点执行 修改之前的记录为已审核和已通知*/
    @Scheduled(cron = "0 0 5 * * ?")
    @Async("cstaskexecutor")
    public void updateAllRecordStatus() {
        try {
            fangKeService.updateAllRecordStatus();
        }catch (Exception e){
            logger.error(Util.toStackTrace(e));
        }
    }
}
